package com.csu.common.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.csu.common.constants.CommonParams;
import com.csu.common.utils.HttpUtil;
import com.csu.common.utils.JsonUtil;
import com.csu.common.vo.BaiduLocationVo;
import com.csu.common.vo.BaiduMapResultVo;
import com.csu.common.vo.MapResultVo;

/**
 * @项目名称：cphone
 * @类名称：BaiduMapHelpper
 * @类描述：百度地图基础服务， 参考地址：
 *                http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice
 *                -geocoding
 * @创建人：Tangc
 * @创建时间：2017年10月8日 上午11:23:34
 * @company:
 */
public class BaiduMapHelpper
{

    public static MapResultVo position2Address(double lat, double lng)
    {
        Map<String, Object> params = new HashMap<String, Object>();
        String location = lat + "," + lng;
        params.put("location", location);

        String result = baiduGeo(CommonParams.BAIDU_Position2Address_URL, CommonParams.BAIDU_OUTPUT,
                CommonParams.BAIDU_CALLBACK, params);
        String json = extractMessage(result).get(0);

        BaiduMapResultVo baiduMapResultVo = JsonUtil.toObject(json, BaiduMapResultVo.class);

        if (baiduMapResultVo == null)
        {
            return new MapResultVo();
        }
        return baiduMapResultVo.getResult();
    }

    public static String address2Position(String address, String city)
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("address", address);
        if (city != null)
        {
            params.put("city", city);
        }

        String result = baiduGeo(CommonParams.BAIDU_Position2Address_URL, CommonParams.BAIDU_OUTPUT, null, params);
        BaiduLocationVo location = JsonUtil.toObject(result, BaiduLocationVo.class);

        String position = location.getResult().getLocation().getLat() + ","
                + location.getResult().getLocation().getLng();
        return position;

    }

    /**
     * 提取中括号中内容，忽略中括号中的中括号
     * 
     * @param msg
     * @return
     */
    public static List<String> extractMessage(String msg)
    {

        List<String> list = new ArrayList<String>();
        int start = 0;
        int startFlag = 0;
        int endFlag = 0;
        for (int i = 0; i < msg.length(); i++)
        {
            if (msg.charAt(i) == '(')
            {
                startFlag++;
                if (startFlag == endFlag + 1)
                {
                    start = i;
                }
            }
            else if (msg.charAt(i) == ')')
            {
                endFlag++;
                if (endFlag == startFlag)
                {
                    list.add(msg.substring(start + 1, i));
                }
            }
        }
        return list;
    }

    private static String baiduGeo(String url, String output, String callback, Map<String, Object> params)
    {
        params.put("output", output);
        if (callback != null)
        {
            params.put("callback", callback);
        }
        params.put("ak", CommonParams.BAIDU_AK);
        params.put("ret_coordtype", "wgs84ll");

        String result = HttpUtil.getMap(url, params);
        return result;
    }
}
